package com.eleadmin.order.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.order.biz.DriverBiz;
import com.eleadmin.order.entity.Driver;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.order.result.SelectResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2023-04-07 13:41:05
 */
@Repository
public class DriverBizImpl extends BaseDao implements DriverBiz {

    @Override
    public JsonResult list(PageResult2<T> page, Driver driver, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
        StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id,suser3.username account");
        sql.append(",d.car_team,d.name,d.pinyin,d.id_card_pic");
        sql.append(",d.id_card,d.phone,d.car_no,d.white_card");
        sql.append(",d.white_card_pic,d.white_card_date,d.white_card_date_start,d.vehicle_license");
        sql.append(",d.vehicle_license_date,d.vehicle_license_date_start,d.drive_license,d.drive_license_date");
        sql.append(",d.drive_license_date_start,d.corporation_license_pic,d.operation_license,d.operation_license_date");
        sql.append(",d.operation_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
        sql.append(",d.qualification_certificate,d.bank_name,d.bank_no,d.urgent_phone");
        sql.append(",d.state,d.type,d.order_num,d.create_time");
        sql.append(",d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from chf_driver d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=d.update_user ");
        sql.append(" left join sys_user suser3 on suser3.customer_id=d.id and suser3.type='司机' ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(driver)) {
            if (Utils.isNotEmpty(driver.getName())) {
                sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin, :name)>0) ");
                map.put("name", driver.getName());
            }
            if (Utils.isNotEmpty(driver.getCarTeam())) {
                sql.append(" and instr(d.car_team,:car_team)>0 ");
                map.put("car_team", driver.getCarTeam());
            }
            if (Utils.isNotEmpty(driver.getCarNo())) {
                sql.append(" and instr(d.car_no,:car_no)>0 ");
                map.put("car_no", driver.getCarNo());
            }
            if (Utils.isNotEmpty(driver.getPhone())) {
                sql.append(" and instr(d.phone,:phone)>0 ");
                map.put("phone", driver.getPhone());
            }
            if (Utils.isNotEmpty(driver.getSort()) && Utils.isNotEmpty(driver.getOrder())) {
                sql.append(" order by d." + driver.getSort() + " " + driver.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Driver> list(Driver driver, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
        StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id,suser3.username account");
        sql.append(",d.car_team,d.name,d.pinyin,d.id_card_pic");
        sql.append(",d.id_card,d.phone,d.car_no,d.white_card");
        sql.append(",d.white_card_pic,d.white_card_date,d.white_card_date_start,d.vehicle_license");
        sql.append(",d.vehicle_license_date,d.vehicle_license_date_start,d.drive_license,d.drive_license_date");
        sql.append(",d.drive_license_date_start,d.corporation_license_pic,d.operation_license,d.operation_license_date");
        sql.append(",d.operation_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
        sql.append(",d.qualification_certificate,d.bank_name,d.bank_no,d.urgent_phone");
        sql.append(",d.state,d.type,d.order_num,d.create_time");
        sql.append(",d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_driver d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" left join sys_user suser3 on suser3.customer_id=d.id ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(driver)) {
            if (Utils.isNotEmpty(driver.getName())) {
                sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin, :name)>0) ");
                map.put("name", driver.getName());
            }
            if (Utils.isNotEmpty(driver.getCarTeam())) {
                sql.append(" and instr(d.car_team,:car_team)>0 ");
                map.put("car_team", driver.getCarTeam());
            }
            if (Utils.isNotEmpty(driver.getCarNo())) {
                sql.append(" and instr(d.car_no,:car_no)>0 ");
                map.put("car_no", driver.getCarNo());
            }
            if (Utils.isNotEmpty(driver.getPhone())) {
                sql.append(" and instr(d.phone,:phone)>0 ");
                map.put("phone", driver.getPhone());
            }
            if (Utils.isNotEmpty(driver.getSort()) && Utils.isNotEmpty(driver.getOrder())) {
                sql.append(" order by d." + driver.getSort() + " " + driver.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSelCarNo(Driver driver, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select d.id value, d.car_no label");
        sql.append(" ,d.name ,d.phone phone,d.car_no carNo,d.white_card whiteCard ");
        sql.append(" from chf_driver d ");
        sql.append(" where d.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(driver)) {
            if (Utils.isNotEmpty(driver.getName())) {
                sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin, :name)>0) ");
                map.put("name", driver.getName());
            }
            if (Utils.isNotEmpty(driver.getCarTeamId())) {
                sql.append(" and d.car_team_id=:car_team_id ");
                map.put("car_team_id", driver.getCarTeamId() + "");
            }
            if (Utils.isNotEmpty(driver.getCarTeam())) {
                sql.append(" and instr(d.car_team,:car_team)>0 ");
                map.put("car_team", driver.getCarTeam());
            }
            if (Utils.isNotEmpty(driver.getCarNo())) {
                sql.append(" and instr(d.car_no,:car_no)>0 ");
                map.put("car_no", driver.getCarNo());
            }
            if (Utils.isNotEmpty(driver.getPhone())) {
                sql.append(" and instr(d.phone,:phone)>0 ");
                map.put("phone", driver.getPhone());
            }
            if (Utils.isNotEmpty(driver.getSort()) && Utils.isNotEmpty(driver.getOrder())) {
                sql.append(" order by d." + driver.getSort() + " " + driver.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
            sql.append(" limit 30 ");
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public List<SelectResult> listSelDriverName(Driver driver, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<SelectResult> rm = new BeanPropertyRowMapper<SelectResult>(SelectResult.class);
        StringBuffer sql = new StringBuffer("select d.id value, d.name label");
        sql.append(" ,d.phone phone,d.car_no carNo,d.white_card whiteCard ");
        sql.append(" from chf_driver d ");
        sql.append(" where d.deleted=0 ");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(driver)) {
            if (Utils.isNotEmpty(driver.getName())) {
                sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin, :name)>0) ");
                map.put("name", driver.getName());
            }
            if (Utils.isNotEmpty(driver.getCarTeamId())) {
                sql.append(" and d.car_team_id=:car_team_id ");
                map.put("car_team_id", driver.getCarTeamId() + "");
            }
            if (Utils.isNotEmpty(driver.getCarTeam())) {
                sql.append(" and instr(d.car_team,:car_team)>0 ");
                map.put("car_team", driver.getCarTeam());
            }
            if (Utils.isNotEmpty(driver.getCarNo())) {
                sql.append(" and instr(d.car_no,:car_no)>0 ");
                map.put("car_no", driver.getCarNo());
            }
            if (Utils.isNotEmpty(driver.getPhone())) {
                sql.append(" and instr(d.phone,:phone)>0 ");
                map.put("phone", driver.getPhone());
            }
            if (Utils.isNotEmpty(driver.getSort()) && Utils.isNotEmpty(driver.getOrder())) {
                sql.append(" order by d." + driver.getSort() + " " + driver.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
            sql.append(" limit 30 ");
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Driver getById(String id) {
        RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id");
        sql.append(",d.car_team,d.name,d.pinyin,d.id_card_pic");
        sql.append(",d.id_card,d.phone,d.car_no,d.white_card");
        sql.append(",d.white_card_pic,d.white_card_date,d.white_card_date_start,d.vehicle_license");
        sql.append(",d.vehicle_license_date,d.vehicle_license_date_start,d.drive_license,d.drive_license_date");
        sql.append(",d.drive_license_date_start,d.corporation_license_pic,d.operation_license,d.operation_license_date");
        sql.append(",d.operation_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
        sql.append(",d.qualification_certificate,d.bank_name,d.bank_no,d.urgent_phone");
        sql.append(",d.state,d.type,d.order_num,d.create_time");
        sql.append(",d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from chf_driver d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 and d.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public Integer save(Driver driver, User loginUser) {
        GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
        KeyHolder keyHolder = new GeneratedKeyHolder();
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into chf_driver (");
        sql.append("organization_id,car_team_id,car_team,name");
        sql.append(",pinyin,id_card_pic,id_card,phone");
        sql.append(",car_no,white_card,white_card_pic,white_card_date");
        sql.append(",white_card_date_start,vehicle_license,vehicle_license_date,vehicle_license_date_start");
        sql.append(",drive_license,drive_license_date,drive_license_date_start,corporation_license_pic");
        sql.append(",operation_license,operation_license_date,operation_license_date_start,insurance_license");
        sql.append(",insurance_license_date,insurance_license_date_start,qualification_certificate,bank_name");
        sql.append(",bank_no,urgent_phone,state,type");
        sql.append(",order_num,create_user,create_time) values ( ");
        sql.append(":organization_id,:car_team_id,:car_team,:name");
        sql.append(",:pinyin,:id_card_pic,:id_card,:phone");
        sql.append(",:car_no,:white_card,:white_card_pic,:white_card_date");
        sql.append(",:white_card_date_start,:vehicle_license,:vehicle_license_date,:vehicle_license_date_start");
        sql.append(",:drive_license,:drive_license_date,:drive_license_date_start,:corporation_license_pic");
        sql.append(",:operation_license,:operation_license_date,:operation_license_date_start,:insurance_license");
        sql.append(",:insurance_license_date,:insurance_license_date_start,:qualification_certificate,:bank_name");
        sql.append(",:bank_no,:urgent_phone,:state,:type");
        sql.append(",:order_num,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("car_team_id", driver.getCarTeamId() == null ? null : driver.getCarTeamId() + "");
        map.put("car_team", driver.getCarTeam());
        map.put("name", driver.getName());
        map.put("pinyin", driver.getPinyin());
        map.put("id_card_pic", driver.getIdCardPic());
        map.put("id_card", driver.getIdCard());
        map.put("phone", driver.getPhone());
        map.put("car_no", driver.getCarNo());
        map.put("white_card", driver.getWhiteCard());
        map.put("white_card_pic", driver.getWhiteCardPic());
        map.put("white_card_date", driver.getWhiteCardDate());
        map.put("white_card_date_start", driver.getWhiteCardDateStart());
        map.put("vehicle_license", driver.getVehicleLicense());
        map.put("vehicle_license_date", driver.getVehicleLicenseDate());
        map.put("vehicle_license_date_start", driver.getVehicleLicenseDateStart());
        map.put("drive_license", driver.getDriveLicense());
        map.put("drive_license_date", driver.getDriveLicenseDate());
        map.put("drive_license_date_start", driver.getDriveLicenseDateStart());
        map.put("corporation_license_pic", driver.getCorporationLicensePic());
        map.put("operation_license", driver.getOperationLicense());
        map.put("operation_license_date", driver.getOperationLicenseDate());
        map.put("operation_license_date_start", driver.getOperationLicenseDateStart());
        map.put("insurance_license", driver.getInsuranceLicense());
        map.put("insurance_license_date", driver.getInsuranceLicenseDate());
        map.put("insurance_license_date_start", driver.getInsuranceLicenseDateStart());
        map.put("qualification_certificate", driver.getQualificationCertificate());
        map.put("bank_name", driver.getBankName());
        map.put("bank_no", driver.getBankNo());
        map.put("urgent_phone", driver.getUrgentPhone());
        map.put("state", driver.getState());
        map.put("type", driver.getType());
        map.put("order_num", driver.getOrderNum() == null ? null : driver.getOrderNum() + "");
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
            @Override
            public boolean hasValue(String paramName) {
                return map.containsKey(paramName);
            }

            @Override
            public Object getValue(String paramName) throws IllegalArgumentException {
                return map.get(paramName);
            }
        }, generatedKeyHolder);

        Integer id = generatedKeyHolder.getKey().intValue();
        return id;
    }

    @Override
    public boolean update(Driver driver, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update chf_driver set ");
        sql.append("car_team_id=:car_team_id,car_team=:car_team,name=:name ");
        sql.append(",pinyin=:pinyin,id_card_pic=:id_card_pic,id_card=:id_card ");
        sql.append(",phone=:phone,car_no=:car_no,white_card=:white_card ");
        sql.append(",white_card_pic=:white_card_pic,white_card_date=:white_card_date,white_card_date_start=:white_card_date_start ");
        sql.append(",vehicle_license=:vehicle_license,vehicle_license_date=:vehicle_license_date,vehicle_license_date_start=:vehicle_license_date_start ");
        sql.append(",drive_license=:drive_license,drive_license_date=:drive_license_date,drive_license_date_start=:drive_license_date_start ");
        sql.append(",corporation_license_pic=:corporation_license_pic,operation_license=:operation_license,operation_license_date=:operation_license_date ");
        sql.append(",operation_license_date_start=:operation_license_date_start,insurance_license=:insurance_license,insurance_license_date=:insurance_license_date ");
        sql.append(",insurance_license_date_start=:insurance_license_date_start,qualification_certificate=:qualification_certificate,bank_name=:bank_name ");
        sql.append(",bank_no=:bank_no,urgent_phone=:urgent_phone,state=:state ");
        sql.append(",type=:type,order_num=:order_num,update_time=now() ");
        sql.append(",update_user=:update_user where id=:id ");
        map.put("car_team_id", driver.getCarTeamId() == null ? null : driver.getCarTeamId() + "");
        map.put("car_team", driver.getCarTeam());
        map.put("name", driver.getName());
        map.put("pinyin", driver.getPinyin());
        map.put("id_card_pic", driver.getIdCardPic());
        map.put("id_card", driver.getIdCard());
        map.put("phone", driver.getPhone());
        map.put("car_no", driver.getCarNo());
        map.put("white_card", driver.getWhiteCard());
        map.put("white_card_pic", driver.getWhiteCardPic());
        map.put("white_card_date", driver.getWhiteCardDate());
        map.put("white_card_date_start", driver.getWhiteCardDateStart());
        map.put("vehicle_license", driver.getVehicleLicense());
        map.put("vehicle_license_date", driver.getVehicleLicenseDate());
        map.put("vehicle_license_date_start", driver.getVehicleLicenseDateStart());
        map.put("drive_license", driver.getDriveLicense());
        map.put("drive_license_date", driver.getDriveLicenseDate());
        map.put("drive_license_date_start", driver.getDriveLicenseDateStart());
        map.put("corporation_license_pic", driver.getCorporationLicensePic());
        map.put("operation_license", driver.getOperationLicense());
        map.put("operation_license_date", driver.getOperationLicenseDate());
        map.put("operation_license_date_start", driver.getOperationLicenseDateStart());
        map.put("insurance_license", driver.getInsuranceLicense());
        map.put("insurance_license_date", driver.getInsuranceLicenseDate());
        map.put("insurance_license_date_start", driver.getInsuranceLicenseDateStart());
        map.put("qualification_certificate", driver.getQualificationCertificate());
        map.put("bank_name", driver.getBankName());
        map.put("bank_no", driver.getBankNo());
        map.put("urgent_phone", driver.getUrgentPhone());
        map.put("state", driver.getState());
        map.put("type", driver.getType());
        map.put("order_num", driver.getOrderNum() == null ? null : driver.getOrderNum() + "");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", driver.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update chf_driver set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
